package com.example.remotecontrol.ui

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.remotecontrol.R
import com.example.remotecontrol.ui.devices.DevicesScreen
import com.example.remotecontrol.ui.remote.RemoteScreen
import com.example.remotecontrol.ui.scenes.ScenesScreen
import com.example.remotecontrol.ui.settings.SettingsScreen

@Composable
fun MainScreen() {
    val navController = rememberNavController()
    
    val items = listOf(
        Screen.Remote,
        Screen.Devices,
        Screen.Scenes,
        Screen.Settings
    )
    
    Scaffold(
        bottomBar = {
            NavigationBar {
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                val currentDestination = navBackStackEntry?.destination
                
                items.forEach { screen ->
                    NavigationBarItem(
                        icon = { Icon(painterResource(id = screen.icon), contentDescription = null) },
                        label = { Text(stringResource(id = screen.resourceId)) },
                        selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                        onClick = {
                            navController.navigate(screen.route) {
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true
                            }
                        }
                    )
                }
            }
        }
    ) { innerPadding ->
        NavHost(
            navController = navController,
            startDestination = Screen.Remote.route,
            modifier = Modifier.padding(innerPadding)
        ) {
            composable(Screen.Remote.route) { RemoteScreen() }
            composable(Screen.Devices.route) { DevicesScreen() }
            composable(Screen.Scenes.route) { ScenesScreen() }
            composable(Screen.Settings.route) { SettingsScreen() }
        }
    }
}

sealed class Screen(val route: String, val resourceId: Int, val icon: Int) {
    object Remote : Screen("remote", R.string.tab_remote, R.drawable.ic_remote)
    object Devices : Screen("devices", R.string.tab_devices, R.drawable.ic_devices)
    object Scenes : Screen("scenes", R.string.tab_scenes, R.drawable.ic_scenes)
    object Settings : Screen("settings", R.string.tab_settings, R.drawable.ic_settings)
} 